import {createRouter, createWebHistory} from "vue-router/dist/vue-router";
//import {getCurrentInstance} from "vue";
import VueCookies from "vue-cookies";
import {ElMessage} from "element-plus";
import AES from '@/utils/AESUtil'

const routes = [
    {
        path: '/',
        name: 'login', //登录界面
        component: () => import('@/components/common/login')
    },
    {
        path: '/adminManager', //管理员个人信息主页
        meta:{isAuth:true,auth: 'admin'},
        component: () => import('@/components/admin/adminManager'),
        children: [
            {
                path: '/admin/userInfo', //管理员信息查询
                meta:{isAuth:true,auth: 'admin'},
                component: () => import('@/components/admin/userInfo')
            },
            {
                path: '/admin/changeInfo', //管理员信息修改
                meta:{isAuth:true,auth: 'admin'},
                component: () => import('@/components/admin/changeInfo')
            },
            {
                path: '/admin/changePwd', //管理员密码修改
                meta:{isAuth:true,auth: 'admin'},
                component: () => import('@/components/common/changePwd')
            },
        ]
    },
    {
        path: '/teacherManager', //教师个人信息主页
        meta:{isAuth:true,auth: 'teacher'},
        component: () => import('@/components/teacher/teacherManager'),
        children: [
            {
                path: '/teacher/userInfo', //教师信息查询
                meta:{isAuth:true,auth: 'teacher'},
                component: () => import('@/components/teacher/userInfo')
            },
            {
                path: '/teacher/changeInfo', //教师信息修改
                meta:{isAuth:true,auth: 'teacher'},
                component: () => import('@/components/teacher/changeInfo')
            },
            {
                path: '/teacher/changePwd', //教师密码修改
                meta:{isAuth:true,auth: 'teacher'},
                component: () => import('@/components/common/changePwd')
            },
        ]
    },
    {
        path: '/manage', //教师与管理员主页
        meta:{isAuth:true,auth: 'manage'},
        component: () => import('@/components/manage/index'),
        children: [
            {
                path: '/examDescription', //考试管理功能描述
                meta:{isAuth:true,auth: 'manage'},
                component: () => import('@/components/manage/exam/examDescription')
            },
            {
                path: '/selectExam', //查询所有考试
                meta:{isAuth:true,auth: 'manage'},
                component: () => import('@/components/manage/exam/selectExam')
            },
            {
                path: '/addExam', //添加考试
                meta:{isAuth:true,auth: 'manage'},
                component: () => import('@/components/manage/exam/addExam')
            },
            {
                path: '/paperDescription', //题库管理功能介绍
                meta:{isAuth:true,auth: 'manage'},
                component: ()=> import('@/components/manage/paper/paperDescription')
            },
            {
                path: '/selectPaper', //查询所有题库
                meta:{isAuth:true,auth: 'manage'},
                component: () => import('@/components/manage/paper/selectPaper')
            },
            {
                path: '/selectAnswer', //查询所有题库
                meta:{isAuth:true,auth: 'manage'},
                component: () => import('@/components/manage/paper/selectAnswer')
            },
            {
                path: '/lookPaperAnswer', //查看某个题库
                meta:{isAuth:true,auth: 'manage'},
                component: () => import('@/components/manage/paper/lookPaperAnswer'),
            },
            {
                path: '/add', //增加题库主界面
                meta:{isAuth:true,auth: 'manage'},
                component: () => import('@/components/manage/paper/add'),
            },
            {
                path: '/studentsGrade', //学生成绩统计
                meta:{isAuth:true,auth: 'manage'},
                component: () => import('@/components/manage/charts/studentsGrade')
            },
            {
                path:'/grade', //学生成绩
                meta:{isAuth:true,auth: 'manage'},
                component: () => import('@/components/manage/charts/grade')
            },
            {
                path: '/selectExamPart', //学生分数段
                meta:{isAuth:true,auth: 'manage'},
                component: () => import('@/components/manage/charts/selectExamPart')
            },
            {
                path: '/scorePart',
                meta:{isAuth:true,auth: 'manage'},
                component: () => import('@/components/manage/charts/scorePart')
            },
            {
                path: '/studentManage', //学生管理界面
                meta:{isAuth:true,auth: 'manage'},
                component: () => import('@/components/manage/stuManage/studentManage')
            },
            {
                path: '/addStudent', //添加学生
                meta:{isAuth:true,auth: 'manage'},
                component: () => import('@/components/manage/stuManage/addStudent')
            },
            {
                path: '/teacherManage',
                meta:{isAuth:true,auth: 'admin'},
                component: () => import('@/components/admin/teacherManage')
            },
            {
                path: '/addTeacher',
                meta:{isAuth:true,auth: 'admin'},
                component: () => import ('@/components/admin/addTeacher')
            }
        ]
    },
    {
        path: '/student',
        meta:{isAuth:true,auth: 'student'},
        component: () => import('@/components/student/index'),
        children: [
            {
                path:'/myPaperList',
                meta:{isAuth:true,auth: 'student'},
                component: () => import('@/components/student/myPaperList')
            },
            {
                path: '/manager',
                component: () => import('@/components/student/manager'),
                meta:{isAuth:true,auth: 'student'},
                children: [
                    {
                        path: '/student/userInfo',
                        meta:{isAuth:true,auth: 'student'},
                        component: () => import('@/components/student/userInfo')
                    },
                    {
                        path: '/student/changeInfo',
                        meta:{isAuth:true,auth: 'student'},
                        component: () => import('@/components/student/changeInfo')
                    },
                    {
                        path: '/student/changePwd',
                        meta:{isAuth:true,auth: 'student'},
                        component: () => import('@/components/common/changePwd')
                    },
                    {
                        path: '/student/myMessage',
                        meta:{isAuth:true,auth: 'student'},
                        component: () => import('@/components/student/myMessage')
                    },
                    {
                        path: '/student/myReplay',
                        meta:{isAuth:true,auth: 'student'},
                        component: () => import('@/components/student/myReplay')
                    }
                ]
            },
            {path: '/examMsg',meta:{isAuth:true,auth: 'student'}, component: () => import('@/components/student/examMsg')},
            {path: '/message',meta:{isAuth:true,auth: 'student'}, component: () => import('@/components/student/message')},
            {path: '/studentScore', component: () => import("@/components/student/answerScore")},
            {path: '/scoreTable',meta:{isAuth:true,auth: 'student'}, component: () => import("@/components/student/scoreTable")}
        ]
    },
    {path: '/answer',meta:{isAuth:true,auth: 'student'},component: () => import('@/components/student/answer')}
];
const router = createRouter({
    history:createWebHistory(),
    routes
})
//const porxy = getCurrentInstance();
//全局前置路由守卫---初始化的时候和每次路由切换之前调用
router.beforeEach((to,from,next)=>{
    let role = AES.Decrypt(VueCookies.get('role'))
    if (from.name === 'login' || to.name === 'login'){
        next()
    }
    if (role === null) {
        ElMessage({
            type: 'error',
            message: '未登录，请使用账号登录',
            center: true
        })
        router.push("/")
    }
    if (to.meta) {
        if (to.meta.auth === 'student') {
            if (role === '2') {
                next()
            } else {
                ElMessage({
                    type: 'error',
                    message: '您不是学生，不能访问',
                    center: true
                })
                router.back()
            }
        } else if (to.meta.auth === 'manage') {
             if (role === '1' || role === '0') {
                 next()
            } else {
                 ElMessage({
                     type: 'error',
                     message: '您无权访问该页面',
                     center: true
                 })
                 router.back()
            }
        }else if (to.meta.auth === 'teacher'){
            if (role === '1'){
                next()
            } else {
                ElMessage({
                    type: 'error',
                    message: '您无权访问该页面',
                    center: true
                })
                router.back()
            }
        }else if (to.meta.auth === 'admin'){
            if (role === '0'){
                next()
            } else {
                ElMessage({
                    type: 'error',
                    message: '您无权访问该页面',
                    center: true
                })
                router.back()
            }
        }
    }
})

export default router